//
//  LYDPopoverSecondView.m
//  HAODE
//
//  Created by LiYinDi on 16/5/26.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "FilterSecondView.h"
#import "FilterSecondTableViewCell.h"
#import "UIViewExt.h"
#import "FilterSecondLevelModel.h"
#define kUIScreenHeight [UIScreen mainScreen].bounds.size.height
#define kUIScreenWidth [UIScreen mainScreen].bounds.size.width
@interface FilterSecondView() <UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,FilterSecondCellDelegate>
{
   NSMutableArray *_listArray;
    
  UITextField *startTimeTextField;
  UITextField *endTimeTextField;
    
    FilterSecondLevelModel *selectedSecondModel;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *theContentView;
@property (nonatomic, strong) UIButton *alphaBtn;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *topLineLabel;

@property (nonatomic, strong) UIView *dateView;
@property (nonatomic, strong) UIButton *dateConfirmBtn;
@property (nonatomic, strong) UIDatePicker *datePicker;
@end
@implementation FilterSecondView

-(instancetype)init:(NSMutableArray *)listArray  secondLevelModel:(FilterSecondLevelModel *)secondLevelModel title:(NSString *)title
{
    self=[super init];
    if (self) {
        _listArray=listArray;
        selectedSecondModel=secondLevelModel;
        [self setup:title];
//        [self setAutoLayout];
    }
    return self;
}

- (void)setup:(NSString *)titleStr
{
   
    _alphaBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [_alphaBtn setBackgroundColor: [UIColor clearColor]];
    [self addSubview:_alphaBtn];
    
    _theContentView=[[UIView alloc] init];
    _theContentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_theContentView];
    
    _cancelBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [_cancelBtn setTitleColor:[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1.0] forState:UIControlStateNormal];
    [_cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_cancelBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [_theContentView addSubview:_cancelBtn];
    
    _titleLabel=[[UILabel alloc] init];
    _titleLabel.text=titleStr;
    _titleLabel.textColor=[UIColor blackColor];
    [_titleLabel setFont:[UIFont systemFontOfSize:17]];
    [_theContentView addSubview:_titleLabel];
    
    _topLineLabel=[[UILabel alloc] init];
    [_topLineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [_theContentView addSubview:_topLineLabel];
    
    _tableView = [[UITableView alloc] init];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.bounces = NO;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_theContentView addSubview:_tableView];
    
    _dateView=[[UIView alloc] init];
    [self addSubview:_dateView];
    
    _dateConfirmBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [_dateConfirmBtn setBackgroundColor:[UIColor colorWithRed:90.0f/255.0f green:130.0f/255.0f blue:26.0f/255.0f alpha:1.0f]];
    [_dateConfirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [_dateConfirmBtn setTintColor:[UIColor whiteColor]];
    [_dateConfirmBtn addTarget:self action:@selector(dateConfirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_dateView addSubview:_dateConfirmBtn];
    
    _datePicker = [[UIDatePicker alloc] init];
    [_datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
    _datePicker.datePickerMode = UIDatePickerModeDate;
    _datePicker.backgroundColor=[UIColor whiteColor];
    [_dateView addSubview:_datePicker];
    _dateView.hidden=YES;
    
}
-(void)cancelBtnClick
{
    [self dismiss];
}
-(void)dateConfirmBtnClick
{
    _dateView.hidden=YES;
    NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];
    if ([startTimeTextField isFirstResponder])
    {
        startTimeTextField.text = [NSString stringWithFormat:@"%@", [df stringFromDate:_datePicker.date]];
        
    }else{
        
        endTimeTextField.text = [NSString stringWithFormat:@"%@", [df stringFromDate:_datePicker.date]];
    }

    
}
- (void)dateChanged:(id)sender
{
    
    UIDatePicker* picker = (UIDatePicker*)sender;
    NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];
    
    
    if ([startTimeTextField isFirstResponder])
    {
        startTimeTextField.text = [NSString stringWithFormat:@"%@", [df stringFromDate:picker.date]];
        
    }else{
        
        endTimeTextField.text = [NSString stringWithFormat:@"%@", [df stringFromDate:picker.date]];
    }
    
    
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField.tag==100) {
    
        startTimeTextField=textField;
        
    }
    else
    {
        endTimeTextField=textField;
    }
     _dateView.hidden=NO;
    return YES;
    
}

- (void)dismiss
{
    //    [self.coverBtn removeFromSuperview];
    [self removeFromSuperview];
    
}

#pragma mark - tableView代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * const  CellIdentifier = @"FilterTableViewCellIdentifier";
    FilterSecondTableViewCell *cell=( FilterSecondTableViewCell *)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
    if (cell==nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"FilterSecondTableViewCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
    }
    FilterSecondLevelModel *model=_listArray[indexPath.row];
    cell.delegate=self;
    cell.timeConfirmBtn.tag=indexPath.row;
    cell.model=model;
    cell.startTimeTextField.delegate=self;
    cell.endTimeTextField.delegate=self;
    
    cell.startTimeTextField.inputView = [[UIView alloc]initWithFrame:CGRectZero];
    cell.endTimeTextField.inputView = [[UIView alloc]initWithFrame:CGRectZero];
    
    cell.startTimeTextField.text=selectedSecondModel.minValue;
     cell.endTimeTextField.text=selectedSecondModel.maxValue;
    
    cell.selectedImageView.hidden=YES;
    if ([model.itemName isEqualToString:selectedSecondModel.itemName]) {
        cell.selectedImageView.hidden=NO;
    }
    
    
    return cell;
    
}
#pragma mark FilterSecondTableViewCellDelegate
-(void)timeConfirmButtonClick:(NSUInteger)tag
{
     FilterSecondLevelModel *model=_listArray[tag];
    model.minValue=startTimeTextField.text;
    model.maxValue=endTimeTextField.text;
    model.itemName=[NSString stringWithFormat:@"%@~%@",startTimeTextField.text,endTimeTextField.text];
    if ([self.delegate respondsToSelector:@selector(FilterSecondView:)]) {
        [self.delegate FilterSecondView: model];
    }
    [self dismiss];

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FilterSecondLevelModel *model=_listArray[indexPath.row];
    model.minValue=@"";
    model.maxValue=@"";
    if ([self.delegate respondsToSelector:@selector(FilterSecondView:)]) {
        [self.delegate FilterSecondView: model];
    }
    [self dismiss];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [_alphaBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.top.equalTo(self.mas_top);
        make.bottom.equalTo(self.mas_bottom);
        make.width.equalTo(@30);
    }];
    
    [_theContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_alphaBtn.mas_right);
        make.top.equalTo(self.mas_top);
        make.bottom.equalTo(self.mas_bottom);
        make.right.equalTo(self.mas_right);
    }];
    
    [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_theContentView.mas_left).with.offset(10);
        make.top.equalTo(_theContentView.mas_top).with.offset(25);
        make.height.equalTo(@30);
        make.width.equalTo(@40);
    }];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_theContentView.mas_centerX);
        make.top.equalTo(_theContentView.mas_top).with.offset(25);
        make.height.equalTo(@30);
        make.width.equalTo(@40);
    }];
    
    [_topLineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_theContentView.mas_left);
        make.top.equalTo(_theContentView.mas_top).with.offset(63);
        make.height.equalTo(@1);
        make.right.equalTo(_theContentView.mas_right);
    }];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_theContentView.mas_left);
        make.top.equalTo(_topLineLabel.mas_bottom);
        make.bottom.equalTo(_theContentView.mas_bottom).with.offset(-40);
        make.right.equalTo(_theContentView.mas_right);
    }];
    
    [_dateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.top.equalTo(self.mas_bottom).with.offset(-195);
        make.height.equalTo(@195);
        make.right.equalTo(self.mas_right);
    }];
    [_dateConfirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_dateView.mas_left);
        make.right.equalTo(_dateView.mas_right);
        make.bottom.equalTo(_dateView.mas_bottom);
        make.height.equalTo(@44);
    }];
    [_datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_dateView.mas_left);
        make.right.equalTo(_dateView.mas_right);
        make.bottom.equalTo(_dateConfirmBtn.mas_top);
        make.height.equalTo(@150);
    }];

}

@end
